package 享元模式.代码实现;

import java.util.HashMap;

public class FlyWeightFactory {
	// 池子容器
	private static HashMap<String, FlyWeight> pool = new HashMap<>();

	public static FlyWeight getFlyWeight(String extrinsic) {
		// 需要返回的对象
		FlyWeight flyweight = null;
		if (!pool.containsKey(extrinsic)) {
			// 根据外部状态创建享元对象
			flyweight = new ConcreteFlyweight(extrinsic);
			// 再放入池中
			pool.put(extrinsic, flyweight);
		} else {
			// 池中有对象，则直接返回
			flyweight = pool.get(extrinsic);
		}
		return flyweight;

	}
}
